package com.cristina.attention.utils;

import com.cristina.attention.domain.MessageInfo;
import com.cristina.attention.utils.enums.ChannelType;
import com.cristina.attention.utils.enums.MessageType;

import java.util.ArrayList;
import java.util.List;

/**
 * groupId 标识每一个消费者组
 * @author by superman
 */
public class GroupIdMappingUtils {

    /**
     * 获取全部 groupIds
     * IM_CHAT -> SYNC
     * IM_GROUP -> SYNC
     * IM_PUSH -> NOTICE
     * ...
     * 避免出现永远用不到的 kafka 监听时间, 浪费资源
     *
     * @return
     */
    public static List<String> getAllGroupIds() {
        List<String> groupIdsToIm = new ArrayList<>();
        for (ChannelType channelType : ChannelType.values()) {
            for (MessageType messageType : MessageType.values()) {
                if (messageType.getChannelTypes().indexOf(String.valueOf(channelType.getCode())) == -1) {
                    continue;
                }
                groupIdsToIm.add(channelType.getCodeEn() + "." + messageType.getCodeEn());
            }
        }
        return groupIdsToIm;
    }

    /**
     * 通过 MessageInfo 获取当前消息的 groupId
     * @param messageInfo
     * @return
     */
    public static String getGroupIdByMessageInfo(MessageInfo messageInfo) {
        String imCodeEn = ChannelType.getEnumByCode(messageInfo.getSendChannel()).getCodeEn();
        String msgCodeEn = MessageType.getEnumByCode(messageInfo.getMsgType()).getCodeEn();
        return imCodeEn + "." + msgCodeEn;
    }
}
